/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.PolicyApply;
import org.springblade.modules.enterprise.entity.PolicyApplyFile;
import org.springblade.modules.enterprise.mapper.PolicyApplyMapper;
import org.springblade.modules.enterprise.service.IPolicyApplyFileService;
import org.springblade.modules.enterprise.service.IPolicyApplyService;
import org.springblade.modules.enterprise.vo.PolicyApplyStatusChartsVO;
import org.springblade.modules.enterprise.vo.PolicyApplyVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-08-20
 */
@Slf4j
@AllArgsConstructor
@Service
public class PolicyApplyServiceImpl extends BaseServiceImpl<PolicyApplyMapper, PolicyApply> implements IPolicyApplyService {

	private IPolicyApplyFileService policyApplyFileService;

	@Override
	public IPage<PolicyApplyVO> selectPolicyApplyPage(IPage<PolicyApplyVO> page, PolicyApplyVO policyApply) {
		return page.setRecords(baseMapper.selectPolicyApplyPage(page, policyApply));
	}

	/**
	 * 查询政策申请包含 政策信息和企业信息
	 *
	 * @param page        分页
	 * @param policyApply 政策申请
	 * @return 分页政策申请
	 */
	@Override
	public IPage<PolicyApplyVO> selectPageWithPolicyInfoAndCompanyInfo(IPage<PolicyApplyVO> page, PolicyApply policyApply) {
		QueryWrapper<PolicyApply> queryWrapper = new QueryWrapper<>();
		// 逻辑删除
		queryWrapper.eq("pa.is_deleted", 0);
		// 企业账户
		queryWrapper.eq(Func.isNotEmpty(policyApply.getEnterprise()), "enterprise", policyApply.getEnterprise());
		queryWrapper.eq(Func.isNotEmpty(policyApply.getStatus()), "status", policyApply.getStatus());
		queryWrapper.eq(Func.isNotEmpty(policyApply.getMoney()), "money", policyApply.getMoney());
		queryWrapper.eq(Func.isNotEmpty(policyApply.getPolicy()), "policy", policyApply.getPolicy());
		// 根据时间倒叙
		queryWrapper.orderByDesc("create_time");
		return baseMapper.selectPageWithPolicyInfoAndCompanyInfo(page, queryWrapper);
	}

	@Override
	public List<PolicyApplyStatusChartsVO> getPolicyApplyStatusCharts(String userName) {
		return baseMapper.getPolicyApplyStatusCharts(userName);
	}

	// 保存政策申请
	// 保存关联政策申请文件
	@Transactional
	@Override
	public boolean saveOrUpdate(PolicyApply entity) {
		// 1.保存申请文件信息.
		boolean saveOrUpdate = super.saveOrUpdate(entity);

		// 2.清空申请下所有文件信息
		PolicyApplyVO policyApplyVO = (PolicyApplyVO) entity;
		Long policyApplyId = policyApplyVO.getId();
		policyApplyFileService.remove(new QueryWrapper<PolicyApplyFile>().eq("policy_apply", policyApplyId));

		// 判断保存文件列表是否为空
		boolean filesSave = true;
		List<PolicyApplyFile> files = policyApplyVO.getFiles();
		if (Func.isNotEmpty(files)) {
			// 处理文件信息
			files.forEach(policyApplyFile -> {
				// 因为前面是假删除,需要把目前的fileID变成null
				policyApplyFile.setId(null);
				// 关联政策申请
				policyApplyFile.setPolicyApply(policyApplyId);
			});
			// 保存
			filesSave = policyApplyFileService.saveBatch(files);
		}
		return saveOrUpdate && filesSave;
	}
}
